package com.ty.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.annotation.Auth;
import com.ty.entity.Carts;
import com.ty.resp.CommonResp;
import com.ty.service.ICartsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author txy
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/carts")
public class CartsController {

    @Resource
    private ICartsService cartsService;
    @PostMapping("/addCarts")
    @Auth
    public CommonResp addCarts(Carts carts){
        boolean save = cartsService.save(carts);
        CommonResp resp = new CommonResp<>();
        resp.setSuccess(save);
        return resp;
    }
    @GetMapping("/getCartsByUser")
    @Auth(roles = "admin,user")
    public CommonResp getCartsByUser(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "3") Integer size,
                                     String userId){
        CommonResp resp = new CommonResp();
        Page<Carts> carts = cartsService.getCarts(new Page<>(page, size), userId);
        resp.setContent(carts);
        return resp;
    }
    @DeleteMapping("/deleteCartsById")
    @Auth
    public CommonResp deleteCartsById(String id){
        CommonResp resp = new CommonResp();
        resp.setContent(cartsService.removeById(id));
        return resp;
    }
}
